package org.starsource.forum.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Style.Overflow;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.resources.client.CssResource.NotStrict;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import org.starsource.forum.client.domain.CategoryObject;
import org.starsource.forum.client.domain.SessionObject;
import org.starsource.forum.client.domain.UserObject;
import org.starsource.forum.client.handler.CategorySelectionHandler;
import org.starsource.forum.client.handler.SessionUpdateHandler;
import org.starsource.forum.client.handler.SignInHandler;
import org.starsource.forum.client.handler.SignOutHandler;
import org.starsource.forum.client.view.ImageBrowser;
import org.starsource.forum.client.view.Navigation;
import org.starsource.forum.client.view.SessionDetail;
import org.starsource.forum.client.view.SessionList;
import org.starsource.forum.client.view.SignInDialog;
import org.starsource.forum.client.view.TopPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class StarForum implements EntryPoint {
	private static UserObject userObject;

	public static UserObject getUserObject() {
		return userObject;
	}
	private SignInDialog signInDialog = new SignInDialog();
	private DockLayoutPanel outer = binder.createAndBindUi(StarForum.this);
	private ImageBrowser imageBrowser = new ImageBrowser();
	public ImageBrowser getImageBrowser() {
		return this.imageBrowser;
	}
	public enum AppWindow {
		DESKTOP, IMAGE_BROWSER
	}

	  interface Binder extends UiBinder<DockLayoutPanel, StarForum> { }

	interface GlobalResources extends ClientBundle {
		@NotStrict
		@Source("view/global.css")
		CssResource css();
	}

	private static StarForum instance;
	public static StarForum getInstance() {
		return instance;
	}

	private static final Binder binder = GWT.create(Binder.class);

	@UiField TopPanel topPanel;
	@UiField SessionList sessionList;
	@UiField SessionDetail sessionDetail;
	@UiField Navigation navigation;

	public StarForum() {
		StarForum.instance = this;
	}

	/**
	 * This method constructs the application user interface by instantiating
	 * controls and hooking up event handler.
	 */
	public void onModuleLoad() {
		// Inject global styles.
		GWT.<GlobalResources>create(GlobalResources.class).css().ensureInjected();
	    signInDialog.setSignInHandler(new SignInHandler() {

			@Override
			public void onSignIn(UserObject userObject) {
				StarForum.userObject = userObject;

				// Create the UI defined in Mail.ui.xml.

				// Get rid of scrollbars, and clear out the window's built-in margin,
				// because we want to take advantage of the entire client area.
				Window.enableScrolling(false);
				Window.setMargin("0px");

				// Special-case stuff to make topPanel overhang a bit.
				Element topElem = outer.getWidgetContainerElement(topPanel);
				topElem.getStyle().setZIndex(2);
				topElem.getStyle().setOverflow(Overflow.VISIBLE);

				// Listen for item selection, displaying the currently-selected item in
				// the detail area.
				sessionList.setListener(new SessionList.Listener() {

					@Override
					public void onItemSelected(SessionObject item) {
								sessionDetail.setItem(item);
					}

				});

				navigation.getCategories().setCategorySelectionHandler(new CategorySelectionHandler() {

					@Override
					public void onCategorySelection(CategoryObject categoryObject) {
						sessionList.setCategory(categoryObject);
					}

				});

				sessionDetail.setSessionUpdateHandler(new SessionUpdateHandler() {

					@Override
					public void onSessionUpdate(SessionObject SessionObject) {
						sessionList.updateData();
					}

				});

				topPanel.setSignOutHandler(new SignOutHandler() {

						@Override
						public void onSignOut(UserObject userObject) {
							Cookies.removeCookie("account");
							Cookies.removeCookie("authkey");

							RootLayoutPanel.get().clear();
							signInDialog.clearUserInput();
							signInDialog.center();
							signInDialog.focus();
						}

				    });

				topPanel.setWelcome(userObject.getName());
				sessionList.setCategory(null);

//				sessionList.setCategory(null);

				// Add the outer panel to the RootLayoutPanel, so that it will be
				// displayed.
				switchWindow(AppWindow.DESKTOP);
			}

	    });


		signInDialog.clearUserInput();
	    signInDialog.center();
	    signInDialog.focus();
	    signInDialog.authenticate();

	}

	public void switchWindow(AppWindow window) {
		RootLayoutPanel root = RootLayoutPanel.get();
		switch (window) {
		case DESKTOP:
			root.clear();
			root.add(outer);
			break;
		case IMAGE_BROWSER:
			root.clear();
			root.add(imageBrowser);
//			imageBrowser.setImage();
			break;
		}
	}
}
